package com.cykj.controller;

import com.cykj.dto.ResponseDto;
import com.cykj.mapper.AdminMapper;
import com.cykj.pojo.PsAdmin;
import com.cykj.service.AdminService;
import com.cykj.service.Impl.AdminServiceImpl;
import com.cykj.util.JwtUtils;
import com.cykj.vo.AdminVO;
import com.cykj.vo.LoginVo;
import com.cykj.vo.QueryAdminRoleOrConsultantVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @BelongsProject: Psychology
 * @BelongsPackage: com.cykj.controller
 * @Author: 小林
 * @CreateTime: 2025-01-16 22:32
 * @Description: 管理员
 * @Version: 1.0
 */
@RestController
@RequestMapping("/admin")
public class AdminController {



    @Autowired
    private AdminServiceImpl adminService;

    /**
     * 登录
     * @param httpSession
     * @param vo
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResponseDto login(HttpSession httpSession, @RequestBody LoginVo vo){
        String code =(String) httpSession.getAttribute("code");
        if(vo.getCode().equals(code)){
            return adminService.login(vo.getAccount(), vo.getPwd(), vo.getCode());
        }else {
            return ResponseDto.error("验证码出错");
        }
    }

    /**
     * 获取列表
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping("/getAll")
    public ResponseDto getAll(Integer pageSize,Integer pageNum){
        return adminService.getAll(pageSize, pageNum);
    }

    /**
     * 获取管理员和咨询师全部信息
     * @param vo
     * @return
     */
    @RequestMapping("/getAdminOrConsultantAll")
    public ResponseDto getAdminOrConsultant(@RequestBody QueryAdminRoleOrConsultantVo vo){
        return adminService.getAdminOrConsultant(vo);
    }

    /**
     * 更新管理员状态接口
     * @param id 管理员 ID
     * @param status 新的状态
     * @return 更新结果的响应对象
     */
    @RequestMapping("/updateAdminStatus")
    public ResponseDto updateAdminStatus(@RequestParam("id") Integer id,@RequestParam("status")  Integer status){
        return adminService.updateAdminStatus(id, status);
    }
    /**
     * 重置管理员密码接口
     * @param id 管理员 ID
     * @return 重置结果的响应对象
     */
    @RequestMapping("/resetAdminPassword")
    public ResponseDto resetAdminPassword(Integer id, String newPassword){
        newPassword = "123456";
        return adminService.resetAdminPassword(id, newPassword);
    }
    /**
     * 删除管理员接口
     * @param id 管理员 ID
     * @return 删除结果的响应对象
     */
    @RequestMapping("/updateAdminDelete")
    public ResponseDto updateAdminDelete(@RequestParam("id") Integer id){
        return adminService.updateAdminDelete(id);
    }
    /**
     * 添加管理员或咨询师接口
     * @param vo 管理员或咨询师信息对象
     * @return 添加结果的响应对象
     */
    @RequestMapping("/addAdminOrConsultant")
    public ResponseDto addAdminOrConsultant(@RequestBody QueryAdminRoleOrConsultantVo vo){
        return adminService.addAdminOrConsultant(vo);
    }
    /**
     * 根据令牌获取信息接口
     * @param token JWT 令牌
     * @return 解析后的令牌信息的响应对象
     */
    @RequestMapping("/getInformationByToken")
    public ResponseDto getInformationByToken(String token){
         Claims claims = JwtUtils.parseJWT(token);
        System.out.println(claims);
        return ResponseDto.success(claims);
    }
}

